用switch编程实现: y{-1 (x<0); 1 (x>0); 0(x=0)}

来源:百度知道 编辑:UC知道 时间:2024/06/27 15:36:00

用switch的确不是一个好的主意

if(x<0)
y=-1;
else if(x>0)
y=1;
else
y=0;

#include <stdio.h>
void main()
{
int x,y;
printf("请输入x:");
scanf("%d",&x);
switch(x>0)
{

case 1: printf("1\n");break;
case 0: switch(x==0)
{
case 1:printf("0\n");break;
case 0:printf("-1\n");break;}
}
}